How to CURL and avoid timeout death (Twitter Down) [migrated]

Posted by David on Pro Webmasters See other posts from Pro Webmasters or by David
Published on 2012-06-21T17:15:24Z Indexed on 2012/06/21 21:24 UTC
Read the original article Hit count: 216

Filed under:
|

Twitter is down right now, and one of my site's home pages relies on getting data from Twitter (relies is the problem - it should be more of an accessory feature, as it just shows follow count from its feed).

Here's the code in question:

function socials_Twitter_GetFollowerCount($username) {
    $method = function () use ($username) { return file_get_contents('https://api.twitter.com/1/users/show.json?screen_name='.$username.'&include_entities=true'); };
    $json = cache('bmdtwitter', 3600, $method, false);
    $json = json_decode($json, true);

    return intval($json['followers_count']);
}

What is a good way to make it so if Twitter is down (or not responsive for some reasonable amount of time), our site doesn't appear to be down (I think the timeout maybe defaulting to 30-60 seconds or more).

© Pro Webmasters or respective owner

Related posts about twitter

Related posts about memcached